/*
 * Copyright (c) 2004-2005 Massachusetts Institute of Technology. This code was
 * developed as part of the Haystack (http://haystack.lcs.mit.edu/) research
 * project at MIT. Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including without
 * limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
/*
 * Created on Jan 7, 2005
 *
 */
package edu.mit.csail.relo.jdt.builder;

import java.io.IOException;
import java.util.zip.ZipFile;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.IResourceDeltaVisitor;
import org.eclipse.core.resources.IResourceVisitor;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.JavaCore;


class ReloJDTSizeVisitor implements IResourceDeltaVisitor, IResourceVisitor {
    int size = 0;

    /* (non-Javadoc)
     * @see org.eclipse.core.resources.IResourceDeltaVisitor#visit(org.eclipse.core.resources.IResourceDelta)
     */
    public boolean visit(IResourceDelta delta) throws CoreException {
        switch (delta.getKind()) {
        case IResourceDelta.REMOVED :
            return false;
        case IResourceDelta.ADDED :
        case IResourceDelta.CHANGED :
			return visitResource(delta.getResource());
        default:
            return true;
        }
    }

    /* (non-Javadoc)
     * @see org.eclipse.core.resources.IResourceVisitor#visit(org.eclipse.core.resources.IResource)
     */
    public boolean visit(IResource resource) throws CoreException {
        return visitResource(resource);
    }

    private boolean visitResource(IResource resource) {
		if (resource.getType() == IResource.FILE)
			{
				if (resource.getName().endsWith(ReloJDTBuilder.JAVA_SUFFIX))
					{
						size++;
						return true;
					}
				// TODO UNDISABLE JAR and ZIP when we get classfile processing stable
				if (true) return false;
				if(resource.getFileExtension() == null || "jar zip".indexOf(resource.getFileExtension()) < 0)
				{
					return true;
				}
				IFile file = (IFile)resource;
				if(JavaCore.createJarPackageFragmentRootFrom(file) == null) { return true; }
				if("jar zip".indexOf(file.getFileExtension().toLowerCase()) >= 0)
				{
					try
					{
						ZipFile zf = new ZipFile(file.getRawLocation().toOSString());
						size += zf.size();
					}
					catch (IOException e)
					{
						e.printStackTrace();
					}
				}
				return true;
			}
        return true;
    }
}